<?php
/**
 * appRain v 0.1.x
 *
 * LICENSE
 *
 * This source file is subject to the MIT license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/mit-license.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@apprain.com so we can send you a copy immediately.
 *
 * @copyright  Copyright (c) 2010 appRain, Inc. (http://www.apprain.com)
 * @license    http://www.opensource.org/licenses/mit-license.php MIT license
 *
 * HELP
 *
 * Official Website
 * http://www.apprain.com/
 *
 * Download Link
 * http://www.apprain.com/download
 *
 * Documents Link
 * http ://www.apprain.com/documents
 */
class appforumController extends appRain_Base_Core
{
    public $name = 'appForum';

	public function __preDispatch()
    {

    }

    /**
     * This function will reander the home page
     *
     * @return null
     */
    public function indexAction($action = null, $id = null)
    {
		$this->pageNameToMetaInfo('Forum');

		if(!empty($this->data))
		{
			$this->layout = 'empty';

			$capacha = App::Module('Session')->read('capacha');
			if($capacha['commentpost'] != $this->data['Comment']['capacha'])
			{
				echo(App::Module('Cryptography')->jsonEncode(Array("_status"=>"Error","_message"=>"Fille capacha text correclty.")));
			}
			else if($this->data['Comment']['desc'] == "")
			{
				echo(App::Module('Cryptography')->jsonEncode(Array("_status"=>"Error","_message"=>"Enter a comment.")));
			}
			else
			{
				$this->Comment
				     ->setPostid($id)
					 ->setCatid($this->data['Comment']['category'])
					 ->setUserid(App::MemberManager()->AppUserloggedInId())
					 ->setDescription($this->data['Comment']['desc'])
					 ->setDated(date('Y-m-d H:i:s'))
					 ->setStatus('Inactive')
					 ->save();
				    echo(App::Module('Cryptography')->jsonEncode(Array("_status"=>"Success","_message"=>"Reply posted successfully. Waiting for pendig appproval.")));

					$memberData = App::MemberManager()->memberInformation(App::MemberManager()->AppUserloggedInId());
					App::Helper('EmailTemplate')->setParameters(Array(
																	'Name'=>"{$memberData['f_name']} {$memberData['l_name']}",
																	'Comment'=>$this->data['Comment']['desc'],
																	'EmailAddress'=>$memberData['email'],
																	'Website'=>$memberData['website']
															))
								            ->prepare('ForumComment',true);
			}
		}

		$this->addons = Array('syntaxhighlighter','validation.ext','ajaxsubmit');

		if($action == 'c')
		{
			$postlist = App::Informationset('forum-post')->findAll('1 ORDER BY id DESC',Array(Array("status"=>"Active"),'AND',Array('category'=>$id)));
			$this->set('postlist',$postlist);
		}
		else if($action == 'p')
		{
			$postinfo = App::Informationset('forum-post')->findById($id);
			$this->set('postinfo',$postinfo);

			$commetns = $this->Comment->findALl("postid = {$id} AND status='Active'");
			$this->set('commetns',$commetns);

		}
		else
		{
			$man_cat  = App::CategorySet('forum-cat')->findAll();
			$this->set('man_cat',$man_cat);
		}

		$this->set("section_title","Forum");
        $this->set("selected","forum");
		$this->set("action",$action);
		$this->set("id",$id);
    }

	public function submitpostAction()
	{
		$pageInfo = $this->pageNameToMetaInfo('ForumPost');

		/* Manage Post */
		if(!empty($this->data))
		{
			$this->layout = 'empty';
			$capacha = App::Module('Session')->read('capacha');
			if( $capacha['forumpost'] != $this->data["Forum"]['capacha'])
			{
				echo App::Module("Cryptography")->jsonEncode(
						   Array("_status"=>"Error","_message"=>$this->__("Please fillup capacha correctly."))
						 );
			}
			else
			{


				$errors = App::InformationSet('forum-post')
							->setTitle($this->data["Forum"]["title"])
							->setCategory($this->data["Forum"]["cat"])
							->setUserid(App::MemberManager()->AppUserloggedInId())
							->setDescription($this->data["Forum"]["desc"])
							->setStatus('Active')
							->save()
							->getErrorInfo();

				if(empty($errors))
				{
					$memberData = App::MemberManager()->memberInformation(App::MemberManager()->AppUserloggedInId());
					App::Helper('EmailTemplate')->setParameters(Array(
																	'Name'=>"{$memberData['f_name']} {$memberData['l_name']}",
																	'Title'=>$this->data['Forum']['title'],
																	'EmailAddress'=>$memberData['email'],
																	'Website'=>$memberData['website']
															))
								                ->prepare('ForumPost',true);

					echo App::Module("Cryptography")
						->jsonEncode(
						   Array("_status"=>"Success","_message"=>$this->__("Post submittedy Successfully, waiting for pending approval."))
						 );
				}
				else
				{
					echo App::Module("Cryptography")->jsonEncode(
						   Array("_status"=>"Error","_message"=>$this->__("Please fillup the form correclty."))
						 );
				}
			}
		}

		$this->addons = array('validation.ext','ajaxsubmit');

		$man_cat  = App::CategorySet('forum-cat')->findAll();
		$this->set('man_cat',$man_cat);
		$this->set("pageInfo",$pageInfo);
		$this->set("section_title","Forum");
        $this->set("selected","forum");
	}

	public function manageCommentAction($action = NULL, $id = NULL)
	{
		$this->addons = Array('row_manager');
		$this->setAdminTab('blog');

		if(!empty($this->data))
		{
			$this->data['Comment']['id'] = $id;
			$this->Comment->Save($this->data);
			$this->redirect("/appforum/managecomment");///update/{$id}
			exit;
		}

		if($action == 'update')
		{
			$data = App::Model('Comment')->findById($id);
			$this->set("data_list",$data);
		}
		else
		{
			$data = App::Model('Comment')->paging("1 ORDER BY id DESC",5);
			$this->set("data_list",$data);
		}


		$this->set('action',$action);
	}

	public function manageticketAction($action = NULL, $id = NULL)
	{
		$this->addons = Array('row_manager');
		$this->setAdminTab('blog');

		if(!empty($this->data))
		{
			$this->data['Ticket']['id'] = $id;
			$this->Ticket->Save($this->data);
			$this->redirect("/appforum/manageticket/update/{$id}");///
			exit;
		}

		if($action == 'update')
		{
			$data = App::Model('Ticket')->findById($id);
			$this->set("data_list",$data);
		}
		else
		{
			$data = App::Model('Ticket')->paging("1 ORDER BY id DESC",30);
			$this->set("data_list",$data);
		}


		$this->set('action',$action);
	}

}
